using System;
using System.Collections.Generic;
using BankTeller.Infrastructure.Interface;
using System.Windows.Forms;
using BankTeller.BankTellerCommon;
using BankTeller.BankTellerModule.Constants;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI;
using BankTeller.BankTellerModule.Services;

namespace BankTeller.BankTellerModule
{
    public class CustomerWorkItemController : WorkItemController
    {
        [EventPublication(EventTopicNames.StatusUpdate, PublicationScope.Global)]
        public event EventHandler<EventArgs> UpdateStatusTextEvent;
    
        private ToolStripMenuItem editCustomerMenuItem;
        private ToolStripStatusLabel addressLabel;
        private CustomerSummaryView customerSummaryView;

        public ContextService Context
        {
            get { return WorkItem.Services.Get<ContextService>(); }
        }

        public override void Run()
        {
            AddViews();
            
            ExtendMenu();

            UpdateStatusAndUserAddress();

			WorkItem.Activated += WorkItem_ActivatedOrDeactivated;
			WorkItem.Deactivated += WorkItem_ActivatedOrDeactivated;
        }

		private void WorkItem_ActivatedOrDeactivated(object sender, EventArgs e)
		{
			bool visible = WorkItem.Status == WorkItemStatus.Active;

			if (editCustomerMenuItem != null)
			{
				editCustomerMenuItem.Visible = visible;
			}

			if (addressLabel != null)
			{
				addressLabel.Visible = visible;
			}
		}

		private void AddViews()
        {
            customerSummaryView = customerSummaryView ?? WorkItem.Items.AddNew<CustomerSummaryView>();
            WorkItem.Workspaces[WorkspaceNames.ContentWorkspace].Show(customerSummaryView);

            CustomerInfoView customerInfoView = null;

            if (!WorkItem.Items.Contains("CustomerInfoView"))
            {
                customerInfoView = WorkItem.Items.AddNew<CustomerInfoView>("CustomerInfoView");
                WorkItem.Workspaces[WorkspaceNames.CustomerInfoViewWorkspace].Show(customerInfoView);
            }

            if (!WorkItem.Items.Contains("CustomerHeaderView"))
            {
                CustomerHeaderView customerHeaderView = WorkItem.Items.AddNew<CustomerHeaderView>("CustomerHeaderView");
                WorkItem.Workspaces[WorkspaceNames.CustomerHeaderViewWorkspace].Show(customerHeaderView);
            }

            WorkItem.Activate();
            
            if (customerInfoView != null)
                customerInfoView.FocusFirstTab();
        }

        private void ExtendMenu()
        {
            if (editCustomerMenuItem == null)
            {
                editCustomerMenuItem = new ToolStripMenuItem("Edit");

                if (!WorkItem.UIExtensionSites[Properties.Resources.CustomerMenuExtensionSite].Contains(editCustomerMenuItem))
                {
                    WorkItem.UIExtensionSites[Properties.Resources.CustomerMenuExtensionSite].Add(editCustomerMenuItem);

                    WorkItem.Commands[CommandNames.EditCustomer].AddInvoker(editCustomerMenuItem, "Click");
                    WorkItem.Commands[CommandNames.CustomerMouseOver].AddInvoker(customerSummaryView, "MouseHover");
                }
            }
        }

        private void UpdateStatusAndUserAddress()
        {
            OnStatusTextUpdate(new EventArgs<string>(string.Format("Editing {0}, {1}", Context.CurrentCustomer.LastName, Context.CurrentCustomer.FirstName)));

            UpdateUserAddressLabel(Context.CurrentCustomer);
        }

        private void UpdateUserAddressLabel(Customer customer)
        {
            if (addressLabel == null)
            {
                addressLabel = new ToolStripStatusLabel();
                WorkItem.UIExtensionSites[UIExtensionSiteNames.MainStatus].Add(addressLabel);
                addressLabel.Text = customer.Address1;
            }
        }

        protected virtual void OnStatusTextUpdate(EventArgs eventArgs)
        {
            if (UpdateStatusTextEvent != null)
            {
                UpdateStatusTextEvent(this, eventArgs);
            }
        }

        [CommandHandler(CommandNames.CustomerMouseOver)]
        public void OnCustomerEdit(object sender, EventArgs args)
        {
            if (WorkItem.Status == WorkItemStatus.Active)
            {
                Form form = customerSummaryView.ParentForm;

                string tooltipText = "This is customer work item " + WorkItem.ID;
                ToolTip toolTip = new ToolTip();
                toolTip.IsBalloon = true;
                toolTip.Show(tooltipText, form, form.Size.Width - 30, 30, 3000);
            }
        }
    }
}
